package org.fl3r.open2048;

import org.fl3r.open2048.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;
import com.google.analytics.tracking.android.EasyTracker;

public class MainActivity extends Activity {

	// fl3r: Google Analytics

	@Override
	public void onStart() {
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this); // Add this method.
	}

	@Override
	public void onStop() {
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this); // Add this method.
	}

	// fl3r.

	private WebView mWebView;
	private long mLastBackPress;
	private static final long mBackPressThreshold = 3500;
	private static final String IS_FULLSCREEN_PREF = "is_fullscreen_pref";
	private static boolean DEF_FULLSCREEN = true;
	private long mLastTouch;
	private static final long mTouchThreshold = 2000;
	private Toast pressBackToast;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// fl3r: forza la vista verticale

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		// fl3r.

		super.onCreate(savedInstanceState);

		// Check if screen rotation is locked in settings
		boolean isOrientationEnabled = false;
		try {
			isOrientationEnabled = Settings.System.getInt(getContentResolver(),
					Settings.System.ACCELEROMETER_ROTATION) == 1;
		} catch (SettingNotFoundException e) {
		}

		// fl3r: ruota lo schermo in vista verticale

		int screenLayout = getResources().getConfiguration().screenLayout
				& Configuration.SCREENLAYOUT_SIZE_MASK;
		if ((screenLayout == Configuration.SCREENLAYOUT_SIZE_LARGE || screenLayout == Configuration.SCREENLAYOUT_SIZE_XLARGE)
				&& isOrientationEnabled) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}

		setContentView(R.layout.activity_main);

		// Load webview with game
		mWebView = (WebView) findViewById(R.id.mainWebView);
		WebSettings settings = mWebView.getSettings();
		String packageName = getPackageName();
		settings.setJavaScriptEnabled(true);
		settings.setDomStorageEnabled(true);
		settings.setDatabaseEnabled(true);
		settings.setDatabasePath("/data/data/" + packageName + "/databases");

		// If there is a previous instance restore it in the webview
		if (savedInstanceState != null) {
			mWebView.restoreState(savedInstanceState);
		} else {
			mWebView.loadUrl("file:///android_asset/2048/index.html");
		}

		pressBackToast = Toast.makeText(getApplicationContext(),
				R.string.press_back_again_to_exit, Toast.LENGTH_SHORT);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		mWebView.saveState(outState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private boolean isFullScreen() {
		return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
				IS_FULLSCREEN_PREF, DEF_FULLSCREEN);
	}

	@Override
	public void onBackPressed() {
		long currentTime = System.currentTimeMillis();
		if (Math.abs(currentTime - mLastBackPress) > mBackPressThreshold) {
			pressBackToast.show();
			mLastBackPress = currentTime;
		} else {
			pressBackToast.cancel();
			super.onBackPressed();
		}
	}
}
